#include <iostream>
#include <pthread.h>
#include <unistd.h>
void *pthread1(void *arg)
{
    printf("thread 1 returning ... \n");
    int *p = (int *)malloc(sizeof(int));
    *p = 1;
    return (void *)p;
}
void *pthread2(void *arg)
{
    printf("thread 2 returning ... \n");
    int *q = (int *)malloc(sizeof(int));
    *q = 2;
    return (void *)q;
}
void *pthread3(void *arg)
{
    while (1)
    { //
        printf("thread 3 is running ...\n");
        sleep(1);
    }
    return NULL;
}
int main()
{
    pthread_t tid;
    void *ret;
    pthread_create(&tid, NULL, pthread1, NULL);
    pthread_join(tid, &ret);
    printf("pthread1 return tid:%X return code %d\n", tid, *(int *)ret);
    free(ret);

    pthread_create(&tid, NULL, pthread2, NULL);
    pthread_join(tid, &ret);
    printf("pthread2 return tid:%X return code %d\n", tid, *(int *)ret);
    free(ret);

    pthread_create(&tid, NULL, pthread3, NULL);
    sleep(3);
    pthread_cancel(tid);
    pthread_join(tid, &ret);
    printf("thread3 return\n");
    free(ret);
}